import { message } from 'antd';
import {
  getUserList,
  resetUserPwd,
  activateUser,
  freezeUser,
  deleteUser,
  addUser,
  getEmployee,
  getAllRoleList,
  updateUser,
  revalidateSysUser
} from '../services';
import { getRoleList } from '../../commonrolemanage/services';

export default {
  namespace: 'userManageModel',
  state: {
    // value: 123,
    userList: [],
    userListTotal: 0,
  },
  effects: {
    *getUserList({ payload }, { put, call, select }) {
      const res = yield call(getUserList, payload);
      const { code, data, message: errMsg } = res;
      if (code === 0) {
        const { dataList, total } = data;
        yield put({
          type: 'updateModel',
          payload: {
            userList: dataList,
            userListTotal: total,
          },
        });
      } else {
        message.error(errMsg);
      }
    },
    *getRoleSelectList({ payload }, { put, call, select }) {
      return yield call(getRoleList, payload);
    },
    *getEmployeeList({ payload }, { put, call, select }) {
      return yield call(getEmployee, payload);
    },
    *addUser({ payload }, { put, call, select }) {
      return yield call(addUser, payload);
    },
    *updateUser({ payload }, { put, call, select }) {
      return yield call(updateUser, payload);
    },
    *deleteUser({ payload }, { put, call, select }) {
      return yield call(deleteUser, payload);
    },
    *resetUserPwd({ payload }, { put, call, select }) {
      return yield call(resetUserPwd, payload);
    },
    *activateUser({ payload }, { put, call, select }) {
      return yield call(activateUser, payload);
    },
    *freezeUser({ payload }, { put, call, select }) {
      return yield call(freezeUser, payload);
    },
    *getAllRoleList({ payload }, { put, call, select }) {
      return yield call(getAllRoleList, payload);
    },
    *revalidateSysUser({ payload }, { put, call, select }) {
      return yield call(revalidateSysUser, payload);
    },
  },
  reducers: {
    /* 更新状态（通用） */
    updateModel: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
};
